package com.shareach.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.shareach.common.BaseQueryOrder;
import com.shareach.dao.redis.impl.DeployRDao;
import com.shareach.dao.redis.impl.DeployRDao.DeployType;
import com.shareach.domain.BaseCityDomain;
import com.shareach.service.BaseCityService;
import com.shareach.util.JsonUtil;
import com.shareach.web.basic.Answer;

@Controller("wcity")
@RequestMapping("/wcity")
public class CityController extends BaseController {
	@Autowired
	private BaseCityService cityService;
	@Autowired
	private DeployRDao deployRDao;

	@RequestMapping(value = "/index.htm")
	public ModelAndView summary(HttpServletRequest request, String name, String py) {
		ModelAndView mv = new ModelAndView("admin/shop/city/index");

		BaseQueryOrder order = new BaseQueryOrder();
		order.setNumber(getPageNo(request).toString());
		order.setPageSize(getPageSize(request));
		order.addSort("_id", Order.DESCENDING.toString());
		BaseCityDomain cityDomain = new BaseCityDomain();

		if (!StringUtils.isEmpty(name)) {
			cityDomain.setName(name);
			mv.addObject("name", name);
		}

		Page<BaseCityDomain> result = cityService.getPageResult(cityDomain, order);

		mv.addObject("result", result);

		return mv;
	}

	@RequestMapping(value = "/edit.htm")
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response, String cityId,
			BaseCityDomain domain, String curroperator) {
		ModelAndView view = new ModelAndView("admin/shop/city/edit");
		if (domain == null || domain.getCode() == null) {
			if (StringUtils.isNotEmpty(cityId)) {
				// redirect
				Long parabrand = Long.valueOf(cityId);
				BaseCityDomain city = cityService.getById(parabrand);
				view.addObject("city", city);
			} else {
				view.addObject("curroperator", "add");
			}
			return view;
		} else {
			Answer<Object> r = new Answer<Object>();
			try {
				if (StringUtils.isEmpty(curroperator)) {
					Long cityCode = domain.getCode();
					BaseCityDomain city = cityService.getById(cityCode);
					BaseCityDomain change = city.compareTo(domain);
					change.setCode(domain.getCode());
					// edit
					domain.setName(null);// 此属性不可更新
					cityService.update(change);
					// update redis
					resetRedisDeployed(DeployType.City, cityCode);
					this.outText(response, JsonUtil.jsonFromObject(r));
				} else {
					// add
					Long code = domain.getCode();
					BaseCityDomain citydom = cityService.getById(code);
					if (null == citydom) {
						cityService.save(domain);
						// add redis
						deployRDao.addDeploy(DeployType.City, domain.getCode());
						this.outText(response, JsonUtil.jsonFromObject(r));
					}
				}
			} catch (Exception e) {
				r.setCmd(-1);
				r.setMsg("System Error");
				this.outText(response, JsonUtil.jsonFromObject(r));
				e.printStackTrace();
			}
			return view;
		}
	}

	public void resetRedisDeployed(DeployType type, Long id) {
		deployRDao.setUndeployed(type, id);
	}
}
